<template>
	<view class="payment">
		<!-- 订单 -->
		<view class="content">
			<view class="row">
				<view class="text">订单编号:</view>
				<view class="number">{{ orderSn }}</view>
			</view>
			<view class="row">
				<view class="text">订单金额:</view>
				<view class="number">{{ total }}元</view>
			</view>
		</view>
		<!-- 支付方式 -->
		<view class="block">
			<view class="title">选择支付方式</view>
			<view class="list">
				<view class="item" @tap="selectPaytype('alipay')">
					<view class="left">
						<image class="img" src="../../../../static/icons/alipay.png"></image>
					</view>
					<view class="center">支付宝支付</view>
					<view class="right">
						<radio :checked="payType==1" color="#354E44" />
					</view>
				</view>
				<view class="item" @tap="selectPaytype('wxpay')">
					<view class="left">
						<image class="img" src="../../../../static/icons/wxpay.png"></image>
					</view>
					<view class="center">微信支付</view>
					<view class="right">
						<radio :checked="payType==2" color="#354E44" />
					</view>
				</view>
			</view>
		</view>
		<!-- 立即支付 -->
		<view class="btns">
			<view class="btn" @tap="goPay">
				立即支付
			</view>
			<view class="tis">
				<view class="text">
					点击立即支付，即代表您同意
				</view>
				<view class="terms">
					《条款协议》
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getPreOrderById,payConfirm } from "../../../../api/order/index.js";
	import mix from "../../../../mixins/index.js";
	export default {
		mixins: [mix],
		data() {
			return {
				orderList: [], // 订单列表
				orderSn: "", // 订单编号
				payType: 1, // 支付方式 - 1->支付宝；2->微信
				orderId: "", // 参数 - 订单id
			};
		},
		onLoad(options) {
			this.orderId = options.orderId;
			// 判断 - 是否登陆
			if(this.checkLogin()){
				getPreOrderById(options.orderId).then(res=>{
					this.orderList = res.data.data.orderItems;
					this.orderSn = res.data.data.orderBase.orderSn;
				})
			}
		},
		computed:{
			total(){
				return this.orderList.reduce((total,current)=>{
					return total + current.productPrice * current.productQuantity
				},0)
			}
		},
		methods:{
			// 选择 - 支付方式
			selectPaytype(val){
				if(val == "alipay"){
					this.payType = 1
				}else{
					this.payType = 2
				}
			},
			// 立即支付
			goPay(){
				payConfirm({
					orderId:this.orderId,
					payType:this.payType
				}).then(res=>{
					if(res.data.success){
						uni.setStorageSync("total",this.total);
						uni.showToast({
							title: "支付成功"
						});
						setTimeout(function(){
							uni.navigateTo({
								url: "../payResult/payResult"
							})
						},1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
@import "./index.scss"
</style>
